package com.cyan.teamquestionnaire.mappers;

import com.cyan.teamquestionnaire.entity.AnswerSheet;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Mapper
public interface AnswerSheetMapper {

    // name未回答的问卷未分页
    @Select("select qid from answer_sheet where user_name = #{name} and ISNULL(answer)")
    List<String> needToAnswer(String name);

    // name已经回答的问卷未分页
    @Select("select qid from answer_sheet where user_name = #{name} and !ISNULL(answer)")
    List<String> haveAnswered(String name);

    // 插入答卷信息，一开始都是answer=null
    @Insert("insert into answer_sheet(user_name, qid) values(#{name}, #{qid})")
    void insertAnswer(@Param(value = "name") String name, @Param(value = "qid") String qid);

    //回答问卷进行数据库更新
    @Update("update answer_sheet set answer = #{answer} ,ans_time = #{ansTime} where user_name = #{name} and qid =#{qId} ")
    void answerQuestionnaire(AnswerSheet answerSheet);

    //批量查询该问卷的已经回答情况
    @Results({
            @Result(column = "user_name", property = "name"),
            @Result(column = "answer", property = "answer"),
            @Result(column = "ans_time",property = "ansTime")
    })
    @Select("select * from answer_sheet where qid = #{qid} and !ISNULL(answer) and user_name like #{name}")
    List<AnswerSheet> questionnaireAnswered(@Param(value = "qid") String qid,@Param(value = "name") String name);

    //批量查询该问卷的未回答情况
    @Results({
            @Result(column = "user_name", property = "name"),
            @Result(column = "answer", property = "answer"),
            @Result(column = "ans_time",property = "ansTime")
    })
    @Select("select * from answer_sheet where qid = #{qid} and ISNULL(answer) and user_name like #{name}")
    List<AnswerSheet> questionnaireNotAnswered(@Param(value = "qid") String qid,@Param(value = "name") String name);

    //根据qid和name查询问卷回答情况
    @Results({
            @Result(column = "user_name", property = "name"),
            @Result(column = "answer", property = "answer"),
            @Result(column = "ans_time",property = "ansTime")
    })
    @Select("select * from answer_sheet where qid = #{qid} and !ISNULL(answer) and user_name = #{name}")
    AnswerSheet AnswerbyQidName(@Param(value = "qid") String qid,@Param(value = "name") String name);

    @Delete("delete from answer_sheet where qid = #{qid}")
    void deleteAnswerQuestionnaire(@Param(value = "qid") String qid);
}
